#include <stdio.h>

int digitSquareSumArrive89(unsigned long n)
{
	int sum=0, times=0;
	while (times !=2) {
		sum = 0;
		while (n) {
			sum = sum + (n%10)*(n%10);
			n /=10;
		}
		if (sum == 89)times++;
		if (sum == 1)return 0;
		n = sum;
	}
	return 1;
}

int main()
{
	unsigned long i;
	int counter = 0;
	for (i=2; i<10000000; i++) {
		if (digitSquareSumArrive89(i)) counter++;
	}
	printf("%d\n", counter);
}
